#include "head.h"

/*
Usage:
*/
int login_check(int client_fd, struct user_s* user, char* cur_path){
    char user_name[100]={0};
    char password[100]={0};

    printf("1\n");
    strncpy(user_name,user->user_name,100*sizeof(char));
    strncpy(password,user->password,100*sizeof(char));

    printf("%s\n",user_name);
    printf("%s\n",password);
    printf("2\n");
    MYSQL *mysql;
    mysql = mysql_init(NULL);
    if (!mysql_real_connect(mysql, "localhost", "root", "123456", "Netdisk", 0, NULL, 0)) {
        printf( "%s\n", mysql_error(mysql));
    }

    mysql_query(mysql,"PREPARE myStatement FROM 'SELECT password FROM user_info WHERE user_name = ?' ");
    char query[1024]={0};
    printf("3\n");
    sprintf(query,"SET @user_name = '%s'",user_name);
    printf("%s\n",query);
    mysql_query(mysql,query);
    mysql_query(mysql,"EXECUTE myStatement USING @user_name");

    int flag = -1;
    MYSQL_RES *res = mysql_store_result(mysql);
    my_ulonglong count_rows = mysql_num_rows(res);
    printf("rows:%ld\n",count_rows);
    if(count_rows != 1){
        printf("login:用户不存在\n");
        flag = 1; //错误码1：用户不存在
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        return flag;
    }

    MYSQL_ROW row;
    char password2[100]={0};
    row=mysql_fetch_row(res);    //读到第1排，则应该对应数据库中的密码
    strcpy(password2,row[0]);
    printf("password in table: %s\n",password2);

    if(strcmp(password,password2)==0){
        flag = 0;
        strcat(cur_path,"/");
        strcat(cur_path,user_name);
        printf("%s\n",cur_path);
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        printf("login:登录成功\n");
        return flag;
    }else{
        flag = 2;
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        printf("login:密码错误\n");
        return flag;
    }

}

int create_account(int client_fd,struct user_s *user,char * cur_path){
    char user_name[100]={0};
    char password[100]={0};

    printf("1\n");
    strncpy(user_name,user->user_name,100*sizeof(char));
    strncpy(password,user->password,100*sizeof(char));

    printf("%s\n",user_name);
    printf("%s\n",password);
    printf("2\n");
    MYSQL *mysql;
    mysql = mysql_init(NULL);
    if (!mysql_real_connect(mysql, "localhost", "root", "123456", "Netdisk", 0, NULL, 0)) {
        printf( "%s\n", mysql_error(mysql));
    }



    char query[1024]={0};
    int flag = -1;
    sprintf(query,"insert into user_info (user_name,password) values('%s','%s') ",user_name,password);
    printf("%s",query);
    mysql_query(mysql,query);
    const char *error = mysql_error(mysql);
    if(error[0]=='\0'){
        flag = 0;
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        strcat(cur_path,"/");
        strcat(cur_path,user->user_name);

        int user_exist_test=access(cur_path,F_OK);
        if(user_exist_test==-1)
        {
            mkdir(cur_path,0775);
            printf("new user add\n");
        }
        return flag;
    }else{
        flag = 1;
        send(client_fd,&flag,sizeof(int),MSG_NOSIGNAL);
        return flag;
    }
}

int server_login(int client_fd,char* cur_path){
    struct user_s user;

    memset(&user,0,sizeof(user));
    recv(client_fd,&user,sizeof(user),MSG_WAITALL);

    printf("cmd_type:%d\n",user.flag);
    printf("user_name:%s\n",user.user_name);
    printf("password:%s\n",user.password);

    int cmd_flag=user.flag;
    if(cmd_flag == 1){
        int ret = login_check(client_fd,&user,cur_path);
        printf("login ret: %d\n",ret);
        if(ret == 0){
            printf("登陆成功，退出循环\n");
            return 0;
        }else return 1;
    }
    if(cmd_flag == 2){
        create_account(client_fd,&user,cur_path);
        return 1;
    }
    return 2;
}
